import os
import json
from datetime import datetime

def view_simple_results():
    """简单查看结果（不需要额外依赖）"""
    print("\n=== 股票分析系统结果查看（简单版）===\n")
    
    # 检查实时数据
    if os.path.exists('data/realtime_prices.csv'):
        print("最新股票价格：")
        with open('data/realtime_prices.csv', 'r', encoding='utf-8-sig') as f:
            # 只读取最后5行
            lines = f.readlines()[-5:]
            for line in lines:
                print(line.strip())
        print("\n")
    else:
        print("未找到实时价格数据\n")
    
    # 检查日志文件
    if os.path.exists('stock_analysis.log'):
        print("最新日志记录：")
        with open('stock_analysis.log', 'r', encoding='utf-8-sig') as f:
            # 只读取最后10行
            lines = f.readlines()[-10:]
            for line in lines:
                print(line.strip())
        print("\n")
    else:
        print("未找到日志文件\n")
    
    # 检查系统状态
    if os.path.exists('status/system_status.json'):
        print("系统状态：")
        with open('status/system_status.json', 'r', encoding='utf-8-sig') as f:
            status = json.load(f)
            print(json.dumps(status, indent=2, ensure_ascii=False))
    else:
        print("未找到系统状态文件")

def view_full_results():
    """完整查看结果（需要pandas）"""
    try:
        import pandas as pd
        print("\n=== 股票分析系统结果查看（完整版）===\n")
        
        # 检查实时数据
        if os.path.exists('data/realtime_prices.csv'):
            # 使用utf-8-sig编码读取CSV文件
            df = pd.read_csv('data/realtime_prices.csv', encoding='utf-8-sig')
            print("最新股票价格：")
            print(df.to_string(index=False))
            print("\n")
        
        # 检查技术分析结果
        if os.path.exists('data/technical_analysis.csv'):
            df = pd.read_csv('data/technical_analysis.csv', encoding='utf-8-sig')
            print("技术指标分析：")
            print(df.to_string(index=False))
            print("\n")
        
        # 检查回测结果
        if os.path.exists('results/backtest_results.csv'):
            df = pd.read_csv('results/backtest_results.csv', encoding='utf-8-sig')
            print("策略回测结果：")
            print(df.to_string(index=False))
            print("\n")
            
    except ImportError:
        print("\n需要安装pandas才能查看完整分析结果。")
        print("请运行: pip install pandas\n")
        view_simple_results()

if __name__ == "__main__":
    try:
        view_full_results()
    except Exception as e:
        print(f"\n运行完整版时出错: {e}")
        print("切换到简单版...\n")
        view_simple_results() 